package com.quxing.amazon.security;

import com.quxing.amazon.common.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.quxing.amazon.common.StatusConst.ERROR;
import static com.quxing.amazon.common.StatusConst.LOGOUT;

/**
 * @Author zhb
 * @Description 注销处理
 * @Date 2020/11/27 16:52
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class JwtLogoutSuccessHandler implements LogoutSuccessHandler {

    private final JwtUtil jwtUtil;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        try {
            jwtUtil.invalidateJwtToken(request);
        } catch (Exception e) {
            log.error("注销失败:{}", e.getMessage());
            ResponseUtil.writeError(response, ERROR);
            return;
        }
        ResponseUtil.writeSuccess(response, LOGOUT, null);
    }
}
